<?php
/**
 * 用户注册、登录管理
 */
namespace app\index\controller;

use think\Controller;
use think\Request;

class User extends Controller
{
    private $rule = [
                'username'=>'require|max:20',
                'email'=>'require|email',
                'password'=>'require|length:6,20',
                'password2'=>'require|length:6,20|confirm:password',// confirm一致性验证
                'verifycode|验证码'=>'require|captcha',
            ];
    private $rule2 = [
                'username'=>'require|max:20',
                'password'=>'require|length:6,20',
            ];
    // 登录页面
    public function index()
    {
        if (session('?user','','index')) {
            $this->redirect('index/index');
        }
        return $this->fetch('user/login');
    }

    public function login(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            // 提交校验
            $vali = $this->validate($data, $this->rule2);
            if (TRUE !== $vali) {
                // dump($vali);
                // return;
                $this->error('帐号或者密码错误，请检查后重新提交！');
            }
            $db = model('User');
            $res = $db->field('id,username,password,email,code,status')->where(['username'=>$data['username']])->find();
            if (!$res || $res['status'] !== 1) {
                $this->error('该用户不存在，获取用户未被审核通过！');
            }
            $pw = md5($data['password'].$res['code']);
            if ($res['password']!=$pw) {
                $this->error('密码错误！');
            }
            $db->update(
                [
                  'last_login_ip'=>$request->ip(),
                  'last_login_time'=>time(),
                ],
                ['id'=>$res['id']]
            );
            // 保存用户信息，作用域index
            $data = [
                'id' => $res->id,
                'username' => $res->username,
                'email' => $res->email,
                'code' => $res->code,
                'status' => $res->status,
            ];
            session('user',$data,'index');
            // 重定向跳转
            $this->success('登录成功！','index/index');
        }
        if (session('?user','','index')) {
            $this->redirect('index/index');
        }
        return $this->fetch();
    }

    // 注册页面
    public function register(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            // 密码验证
            if ($data['password']!==$data['password2']) {
                $this->error('请输入相同的密码！');
            }
            // validate 验证方式
            /*// 手动校验
            if(!captcha_check($captcha)){
                //验证失败
                $this->error('请填写正确的验证码！');
            }*/
            $vali = $this->validate($data, $this->rule);
            if (TRUE !== $vali) {
                $this->error($vali);
            }

            $data['code'] = mt_rand(100, 10000);
            $data['password'] = md5($data['password'].$data['code']);
            $data['last_login_ip'] = $request->ip();
            unset($data['password2']);
            unset($data['verifycode']);
            try {
                $res = model('user')->add($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success('注册成功！');
            }
            $this->error('注册失败！');
        }
        return $this->fetch('user/register');
    }

    // 退出登录
    public function logout()
    {
        session(null,'index');
        $this->redirect('user/login');
    }
}
